<?php

namespace app\models;

use app\models\user\User;
use Yii;

/**
 * This is the model class for table "user_bao".
 *
 * @property int $id
 * @property int $send_id
 * @property int $received_id
 * @property int $quantity
 * @property string $create_time
 * @property string $update_time
 * @property int $status 1 成功 0 没有处理
 * @property int $final
 * @property int $user_id
 * @property int $is_platform
 * @property float $score
 * @property float $final_id
 */
class UserBao extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'user_bao';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['send_id', 'received_id', 'quantity', 'status', 'final', 'user_id', 'is_platform'], 'integer'],
            [['create_time', 'update_time'], 'safe'],
            [['score', 'temp_score', 'init', 'final_id'], 'number'],

            [['log'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'send_id' => 'Send ID',
            'received_id' => 'Received ID',
            'quantity' => 'Quantity',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
            'status' => 'Status',
            'final' => 'Final',
            'user_id' => 'User ID',
            'is_platform' => 'Is Platform',
            'score' => 'Score',
        ];
    }

    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            $time = date('Y-m-d H:i:s');
            $this->update_time = $time;
            if ($insert) {
                $this->create_time = $time;
            }
            return true;
        }
        return false;
    }

    public function getUser()
    {
        return $this->hasOne(User::class, ['id' => 'user_id']);
    }

    public function getSend()
    {
        return $this->hasOne(User::class, ['id' => 'send_id']);
    }
}
